## figure_one.R - code to generate figure 1
## 2023-07-27 - created file that replicates Security Studies R&R submission
library(rgdal)
library(maps)
library(foreign)
library(RColorBrewer)
library(classInt)
library(viridis)
library(mapproj)

#load Main_data.Rdata
# Set working directory (change path to location of unzipped folder)
setwd()

# Load data
load("main_data.RData")

#select needed data
map_df<-main_data %>% dplyr::select(state.abb, fips, longitude, latitude, lynch_count, kfr_black_pooled_p25) 

###load geodata
gusa <- map_data("state")

gcounty <- map_data("county")

##merge data to centralize county location
head(filter(maps::county.fips, grepl(":", polyname)))

fipstab <-
  transmute(maps::county.fips, fips, county = sub(":.*", "", polyname)) %>%
  unique() %>%
  separate(county, c("region", "subregion"), sep = ",")

gcounty <- left_join(gcounty, fipstab, c("region", "subregion"))

##create bins

#mobility
map_mobility <- map_df %>%
  mutate(mobilityGrouped = cut(kfr_black_pooled_p25, 
                               breaks = c(.25, .3, .35, .4, .45, .7),
                               labels = c(".25", ".3", ".35", ".4", ".52")))

#lynch
map_mobility <- map_mobility %>%
  mutate(lynchGrouped = cut(lynch_count, 
                            breaks = c(1, 2, 5, 10, 15, 40),
                            labels = c("1", "2", "5", "10", "40")))                                       

#join
gcounty_mobility <- left_join(gcounty, map_mobility, "fips") 

#select southern states  
gusa_south <- gusa %>% filter(region == "alabama" | region == "arkansas" | region == "florida" | region == "georgia" | region == "kentucky" | region == "louisiana" | region == "mississippi" | region == "missouri" | region == "north carolina" | region == "south carolina" | region == "tennessee" | region == "texas" | region == "virginia" | region == "west virginia")

###Produce mobility plot
mobility_plot  <- ggplot(filter(gcounty_mobility, region %in% c("alabama", "arkansas", "florida", "georgia", "kentucky", "louisiana",
                                                                "mississippi", "missouri", "north carolina", "south carolina", "tennessee", "texas",
                                                                "virginia", "west virginia"))) +
  geom_polygon(aes(long, lat, group = group, fill = mobilityGrouped),
               color = "grey", size = 0.0001)

mobility_plot<- mobility_plot + geom_polygon(aes(long, lat, group = group),
                                             fill = NA, data = gusa_south, color = "#474747") +
  coord_map("bonne", parameters = 41.6) + ggthemes::theme_map()

mobility_plot <- mobility_plot + scale_fill_grey(start = 0, end = 0.9, na.value = "white", , labels = c(".25", ".3", ".35", ".4", ".52"), na.translate = F,
                                                 guide = guide_legend(
                                                   direction = "horizontal",
                                                   keyheight = unit(2, units = "mm"),
                                                   keywidth = unit(20 / length(labels), units = "mm"),
                                                   title.position = 'top',
                                                   # I shift the labels around, the should be placed 
                                                   # exactly at the right end of each legend key
                                                   title.hjust = 0.5,
                                                   label.hjust = 1,
                                                   nrow = 1,
                                                   byrow = T,
                                                   # also the guide needs to be reversed
                                                   reverse = F,
                                                   label.position = "bottom"
                                                 )
) + theme( legend.position = c(.8, -.1), 
           legend.justification = c("right", "bottom"), 
           legend.box.just = "right", 
           legend.margin = margin(6, 6, 6, 6) )+ labs(fill = "Mobility")




###Produce lynch plot
lynch_plot <- ggplot(filter(gcounty_mobility, region %in% c("alabama", "arkansas", "florida", "georgia", "kentucky", "louisiana",
                                                            "mississippi", "missouri", "north carolina", "south carolina", "tennessee", "texas",
                                                            "virginia", "west virginia"))) +
  geom_polygon(aes(long, lat, group = group, fill = lynchGrouped),
               color = "grey", size = 0.0001)

lynch_plot<- lynch_plot + geom_polygon(aes(long, lat, group = group),
                                       fill = NA, data = gusa_south, color = "#474747") +
  coord_map("bonne", parameters = 41.6) + ggthemes::theme_map()

lynch_plot <- lynch_plot + scale_fill_grey(start = 0.9, end = 0, na.value = "white", , labels = c("1", "2", "5", "10", "36"), na.translate = F,
                                           guide = guide_legend(
                                             direction = "horizontal",
                                             keyheight = unit(2, units = "mm"),
                                             keywidth = unit(20 / length(labels), units = "mm"),
                                             title.position = 'top',
                                             # I shift the labels around, the should be placed 
                                             # exactly at the right end of each legend key
                                             title.hjust = 0.5,
                                             label.hjust = 1,
                                             nrow = 1,
                                             byrow = T,
                                             # also the guide needs to be reversed
                                             reverse = F,
                                             label.position = "bottom"
                                           )
) + theme( legend.position = c(.8, -.1), 
           legend.justification = c("right", "bottom"), 
           legend.box.just = "right", 
           legend.margin = margin(6, 6, 6, 6) )+ labs(fill = "Lynch")



